{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "47408fdd",
   "metadata": {},
   "source": [
    "------\n",
    "* 学习人：莫秀芳\n",
    "* 时间：2021-11-15\n",
    "* week12\n",
    "* 学习线上资源：[python官方文档](https://www.python.org/)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd11a24e",
   "metadata": {},
   "source": [
    "## 本周知识点\n",
    "\n",
    "### 1.字典的一般方法\n",
    "\n",
    "> 1.字典的查询 查询key -> value\n",
    ">>1.先看字典的key是否存在：in查询成员是否存在的重要方法\n",
    ">>2.B.查询\n",
    "> 2.字典的修改\n",
    "> 3.字典的删除\n",
    "> 4.字典的增加(可以创建一个新的字典)(dict_name[key]=value)\n",
    "\n",
    "### 2.字典的进阶方法\n",
    "\n",
    "> 1.字典的初始化\n",
    ">> A.D.fromkeys()\n",
    "\n",
    ">> B.D.setdefault()\n",
    "\n",
    "### 3.字典的实践(一)课本\n",
    "\n",
    "### 4.字典的实践（二）课本\n",
    "\n",
    "### 5.字典的实践（三）课外"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "297bddb9",
   "metadata": {},
   "source": [
    "# 1.字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "6e5fe1cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'Xiaofeng', 'stu1803': 'Lihua'}\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"Xiaofeng\",\n",
    "    \"stu1803\":\"Lihua\"\n",
    "}\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4df35629",
   "metadata": {},
   "source": [
    "# 注释： 左边是key右边是value\n",
    "\"stu1801\":\"ZiQi\"\n",
    "    比如\"stu1801\"是key，\"ZiQi\"是value, 字典的形式是冒号：连接"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2dec229",
   "metadata": {},
   "source": [
    "> 1.字典的查询\n",
    ">> 1.直接查找 dict_name[key]当关键词key不存在时，报错： KeyError: 'stu1804'\n",
    "\n",
    ">> 2.get()方法 D.get(k[.d])当关键词key不存在时，返回 **None**不存在。**不会报错**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e5d12b65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接查找 dict_name[key]\n",
    "stu_info[\"stu1801\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "5d2b2045",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'stu1804'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-35-60a16b46fd22>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"stu1804\"\u001b[0m\u001b[1;33m]\u001b[0m   \u001b[1;31m# 这个是老师做的错误示范\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'stu1804'"
     ]
    }
   ],
   "source": [
    "stu_info[\"stu1804\"]   # 这个是老师做的错误示范"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ec760811",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function get:\n",
      "\n",
      "get(key, default=None, /) method of builtins.dict instance\n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 使用get方法查找\n",
    "help(stu_info.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "043cdaf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.get(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e42cb3aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(stu_info.get(\"stu1804\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6367c2ba",
   "metadata": {},
   "source": [
    "> 2.修改 & 新增 \n",
    ">> 1.先查询 查看key是否存在 使用 in 查询成员\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0de6cd0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 stu1804\n",
    "\"stu1804\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "42d22502",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 stu1803\n",
    "\"stu1803\"in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3ee1d11a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'Xiaofeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果key存在 修改 \"stu1803\":\"Lihua\" --> \"stu1803\":\"Liuyu\"\n",
    "if \"stu1803\" in stu_info:\n",
    "    stu_info[\"stu1803\"] = \"LiuYu\"\n",
    "stu_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15b4e198",
   "metadata": {},
   "source": [
    "> 3.字典的删除\n",
    ">> 1. D.pop()的一般操作如下：\n",
    ">>> 1.先查询是否存在 用in的方式去查询\n",
    "\n",
    ">>> 2.再使用删除的方法\n",
    "\n",
    ">> 2. D.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "f6751020",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "pop expected at least 1 argument, got 0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-39-b08b6b6b7100>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#  不正确， 不同于列表 （删除最后一个）     #这个是错误示范，括号（）里面应该是key值，key值不是随便找的奥，是上面的字典里面的\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: pop expected at least 1 argument, got 0"
     ]
    }
   ],
   "source": [
    "stu_info.pop() #  不正确， 不同于列表 （删除最后一个）     #这个是错误示范，括号（）里面应该是key值，key值不是随便找的奥，是上面的字典里面的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "37516f39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'LiuYu'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.pop('stu1803')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9e2ad030",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'Xiaofeng'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "c3cd4de8",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'stu1804'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-38-9704c439f005>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 如果不先查询key是否存在，有可能报错     # 错误示范，未查询，上面的字典里没有，超过范围，所以显示错误报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'stu1804'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'stu1804'"
     ]
    }
   ],
   "source": [
    "# 如果不先查询key是否存在，有可能报错     # 错误示范，未查询，上面的字典里没有，超过范围，所以显示错误报错\n",
    "stu_info.pop('stu1804')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9ea45051",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function popitem:\n",
      "\n",
      "popitem() method of builtins.dict instance\n",
      "    Remove and return a (key, value) pair as a 2-tuple.\n",
      "    \n",
      "    Pairs are returned in LIFO (last-in, first-out) order.\n",
      "    Raises KeyError if the dict is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.popitem)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34a8d2f5",
   "metadata": {},
   "source": [
    "列表是方括号 字典是尖括号 元组是圆括号\n",
    "\n",
    "popitem一步到位，直接删掉,随机移除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "83a86bed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1801', 'ZiQi')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "cb16e2de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b40e037",
   "metadata": {},
   "source": [
    "## 字典的实践（一）\n",
    "\n",
    "课本P112(也可以去gitee看gitee.com/xzhichao/python/tree/master/jupyter_projects/week12_周一下午班）\n",
    "\n",
    "百度搜hash算法（基本算法，面试的时候一定会考）\n",
    "搜许智超博客(cnblogs.com/xuzhichao/p/11481598.html)\n",
    "\n",
    "为什么dict查找速度这么快？因为dict的实现原理和查字典是一样的。假设字典包含了1万个汉字，我们要查某一个字，一个办法是把字典从第一页往后翻，直到找到我们想要的字为止，这种方法就是在list中查找元素的方法，list越大，查找越慢。\n",
    "\n",
    "第二种方法是先在字典的索引表里（比如部首表）查这个字对应的页码，然后直接翻到该页，找到这个字，无论找哪个字，这种查找速度都非常快，不会随着字典大小的增加而变慢。\n",
    "\n",
    "dict就是第二种实现方式，给定一个名字，比如'Michael'，dict在内部就可以直接计算出Michael对应的存放成绩的“页码”，也就是95这个数字存放的内存地址，直接取出来，所以速度非常快。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "3c840783",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search vowels:花欧式尴尬USB\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 目标：查询 元音字母是否出现在word文本当中，并且统计出现的次数\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search vowels:\")\n",
    "found={}\n",
    "\n",
    "# 字典 初始化 的值\n",
    "# found[\"a\"]=0\n",
    "# found[\"e\"]=0\n",
    "# found[\"i\"]=0\n",
    "# found[\"o\"]=0\n",
    "# found[\"u\"]=0\n",
    "\n",
    "for i in vowels:\n",
    "    # 使用字典的新增 构建初始化字典\n",
    "    found[i] = 0\n",
    "    \n",
    "# 循环用户输入的文本 ， 出现一次元音字符，对应的元音字符+1\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fdd5d94",
   "metadata": {},
   "source": [
    "上面这个会帮我统计元音字母出现了几次"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c2a398e",
   "metadata": {},
   "source": [
    "奥，对啦，如果运行不了会出现*号，说明我们运行按太快啦，那么我们要按上面正方形的中断内核，然后再运行一次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ea0198a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "169d5150",
   "metadata": {},
   "source": [
    "> 字典的进阶方法：初始化所有值 fromkeys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f0367692",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(found.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "66d61ec0",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "#word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "found = found.fromkeys(vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "9fa4cf96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': None, 'e': None, 'i': None, 'o': None, 'u': None}"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "0bd82378",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "#word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "found = found.fromkeys(vowels,10) # 初始化让它的所有的值变为10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "265b068a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 10, 'e': 10, 'i': 10, 'o': 10, 'u': 10}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "07e2e12a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:dyusabfuib\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'e': 0, 'i': 1, 'o': 0, 'u': 2}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "# 初始化 字典\n",
    "found = found.fromkeys(vowels,0)\n",
    "\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11f5acfd",
   "metadata": {},
   "source": [
    "> 字典的进阶方法：初始化特定的值 setdefault()\n",
    "\n",
    ">> 1.查询key是否出现在字典当中\n",
    ">>> 1.出现过，就当没看见，不做任何操作\n",
    ">>> 2.key未出现过，给定key及value(指定的初始化的值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e86ae070",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(found.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e4b83f8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = {\n",
    "    \"百度\":\"baidu.com\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9c960016",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'baidu.com'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url.setdefault(\"百度\",\"https://baidu.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e7b24322",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password = {\n",
    "    \"XiaoMing\":\"222333\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "89c42a86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'123456'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_password.setdefault(\"Xiaohong\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b0aa9edb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'222333'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_password.setdefault(\"XiaoMing\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "07283700",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'XiaoMing': '222333'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_password"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "898f35b1",
   "metadata": {},
   "source": [
    "## 字典的实践（二）\n",
    "\n",
    "* 课本121"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "dc7b41f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:fyauvwhdu\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'u': 2}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 优化：实践（一）\n",
    "\n",
    "vowel = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found.setdefault(i,0)     # 特定情况下 （字典中不存在该元素时，初始化其值）\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3b9199bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e': 1, 'o': 2, 'i': 1, 'a': 1}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{'e':1,'o':2,'i':1,'a':1}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "313dff62",
   "metadata": {},
   "source": [
    " 列表[]中括号\n",
    " \n",
    "字典｛｝大括号\n",
    "\n",
    "元组跟方法都用（）小括号\n",
    "\n",
    "大多函数也都用（）小括号，比如print\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9948afbe",
   "metadata": {},
   "source": [
    "## 字典的实践（三）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "625a9524",
   "metadata": {},
   "source": [
    "# 任务： 查找并取值 ‘Tricia McMillan'\n",
    "\n",
    "people[]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5500d146",
   "metadata": {},
   "source": [
    "不懂就去看智超老师的gitee，他都放在那里的下午班啦"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4859388",
   "metadata": {},
   "source": [
    "作业：（这里的作业布置不全，去看许智超老师的gitee)\n",
    "总结字典的所有方法以及方法的注意事项并用表格\n",
    "markdown表格是 | | | |\n",
    "\n",
    "课后实践：（那个什么广州南方学院的周围的地址啊情况啊）\n",
    "1.请找出id为‘BOHGD588HJ'的信息（第一题如上）\n",
    "2.请找出所有的pois中的字典元素id，存到一个id列表中\n",
    "3.请找出所有的pois中的字典元素的name，存到一个id列表中\n",
    "4.请找出所有的name以及location做一个字典（name_dict={name1:location1.\n",
    "5.请找出所有含有'tel'的元素\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6b8331b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "   广州南方学院 = {'status': '1',\n",
    " 'regeocode': {'roads': [{'id': '020F49F0050461206',\n",
    "    'location': '113.678,23.6284',\n",
    "    'direction': '北',\n",
    "    'name': '博学路',\n",
    "    'distance': '7.63232'},\n",
    "   {'id': '020F49F0050462229',\n",
    "    'location': '113.679,23.6245',\n",
    "    'direction': '北',\n",
    "    'name': '老虎形街',\n",
    "    'distance': '445.159'},\n",
    "   {'id': '020F49F0050462451',\n",
    "    'location': '113.677,23.6347',\n",
    "    'direction': '南',\n",
    "    'name': '中山路',\n",
    "    'distance': '708.299'}],\n",
    "  'roadinters': [{'second_name': '中山路',\n",
    "    'first_id': '020F49F0050461206',\n",
    "    'second_id': '020F49F0050462451',\n",
    "    'location': '113.676794,23.634661',\n",
    "    'distance': '708.299',\n",
    "    'first_name': '博学路',\n",
    "    'direction': '南'}],\n",
    "  'formatted_address': '广东省广州市从化区温泉镇博学路',\n",
    "  'addressComponent': {'city': '广州市',\n",
    "   'province': '广东省',\n",
    "   'adcode': '440117',\n",
    "   'district': '从化区',\n",
    "   'towncode': '440117100000',\n",
    "   'streetNumber': {'number': '24号',\n",
    "    'location': '113.677267,23.629149',\n",
    "    'direction': '西北',\n",
    "    'distance': '129.943',\n",
    "    'street': '博学路'},\n",
    "   'country': '中国',\n",
    "   'township': '温泉镇',\n",
    "   'businessAreas': [[]],\n",
    "   'building': {'name': [], 'type': []},\n",
    "   'neighborhood': {'name': [], 'type': []},\n",
    "   'citycode': '020'},\n",
    "  'aois': [],\n",
    "  'pois': [{'id': 'B0FFG4TAF2',\n",
    "    'direction': '东南',\n",
    "    'businessarea': [],\n",
    "    'address': '温泉镇乌土村松浪队5号(近博学路)',\n",
    "    'poiweight': '0.237309',\n",
    "    'name': '广州南方公寓',\n",
    "    'location': '113.678983,23.627778',\n",
    "    'distance': '102.617',\n",
    "    'tel': '13622768437',\n",
    "    'type': '商务住宅;住宅区;住宅小区'},\n",
    "   {'id': 'B0H6FA18J5',\n",
    "    'direction': '东南',\n",
    "    'businessarea': [],\n",
    "    'address': '从莞深高速(广州南方学院南侧)',\n",
    "    'poiweight': '0.223065',\n",
    "    'name': '保利半山花园',\n",
    "    'location': '113.679358,23.627672',\n",
    "    'distance': '139.054',\n",
    "    'tel': [],\n",
    "    'type': '商务住宅;住宅区;住宅区'},\n",
    "   {'id': 'B00140TWHT',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '笃行路2',\n",
    "    'poiweight': '0.157653',\n",
    "    'name': '教工住宅1号',\n",
    "    'location': '113.678490,23.630432',\n",
    "    'distance': '222.658',\n",
    "    'tel': [],\n",
    "    'type': '商务住宅;住宅区;住宅小区'},\n",
    "   {'id': 'B0FFHRJDM9',\n",
    "    'direction': '东南',\n",
    "    'businessarea': [],\n",
    "    'address': '中大南方学院南门1号',\n",
    "    'poiweight': '0.256152',\n",
    "    'name': '中南公寓',\n",
    "    'location': '113.680349,23.627573',\n",
    "    'distance': '231.723',\n",
    "    'tel': '13711245345',\n",
    "    'type': '商务住宅;住宅区;住宅小区'},\n",
    "   {'id': 'B0HGD588HJ',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '博学路',\n",
    "    'poiweight': '0.221662',\n",
    "    'name': '广州南方学院教学楼4号',\n",
    "    'location': '113.677460,23.630490',\n",
    "    'distance': '242.901',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'},\n",
    "   {'id': 'B0FFHZH0IK',\n",
    "    'direction': '西北',\n",
    "    'businessarea': [],\n",
    "    'address': '博学路广州南方学院综合楼内',\n",
    "    'poiweight': '0.17934',\n",
    "    'name': '地下铁',\n",
    "    'location': '113.677941,23.629029',\n",
    "    'distance': '74.1376',\n",
    "    'tel': [],\n",
    "    'type': '餐饮服务;冷饮店;冷饮店'},\n",
    "   {'id': 'B0HGDU76L2',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '博学路',\n",
    "    'poiweight': '0.221662',\n",
    "    'name': '广州南方学院2号实验楼',\n",
    "    'location': '113.678211,23.630956',\n",
    "    'distance': '280.003',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'},\n",
    "   {'id': 'B0FFKP838K',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '博学路25',\n",
    "    'poiweight': '0.191742',\n",
    "    'name': '教学楼3号(外国语学院)',\n",
    "    'location': '113.677247,23.630947',\n",
    "    'distance': '298.093',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;高等院校'},\n",
    "   {'id': 'B0HGDU8NLD',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '博学路附近',\n",
    "    'poiweight': '0.221662',\n",
    "    'name': '广州南方学院1号实验楼',\n",
    "    'location': '113.678215,23.631370',\n",
    "    'distance': '326.007',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'},\n",
    "   {'id': 'B0HGD58UTU',\n",
    "    'direction': '西北',\n",
    "    'businessarea': [],\n",
    "    'address': '博学路',\n",
    "    'poiweight': '0.191229',\n",
    "    'name': '广州南方学院2号教学楼',\n",
    "    'location': '113.677060,23.631346',\n",
    "    'distance': '346.352',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'},\n",
    "   {'id': 'B00140MR9A',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '温泉大道882号',\n",
    "    'poiweight': '0.450402',\n",
    "    'name': '广州南方学院',\n",
    "    'location': '113.679262,23.632583',\n",
    "    'distance': '471.564',\n",
    "    'tel': '020-87828988;020-61787331',\n",
    "    'type': '科教文化服务;学校;高等院校'},\n",
    "   {'id': 'B0HGDU92JD',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': [],\n",
    "    'poiweight': '0.221662',\n",
    "    'name': '广州南方学院教学楼1号',\n",
    "    'location': '113.677077,23.631783',\n",
    "    'distance': '391.548',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'},\n",
    "   {'id': 'B0HGD592BJ',\n",
    "    'direction': '东北',\n",
    "    'businessarea': [],\n",
    "    'address': '温泉大道886号广州南方学院内',\n",
    "    'poiweight': '0.221662',\n",
    "    'name': '广州南方学院综合楼',\n",
    "    'location': '113.680117,23.631544',\n",
    "    'distance': '392.731',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'},\n",
    "   {'id': 'B0FFKP81F0',\n",
    "    'direction': '西北',\n",
    "    'businessarea': [],\n",
    "    'address': '博学路19号附近',\n",
    "    'poiweight': '0.191742',\n",
    "    'name': '教学楼1号(文学与传媒学院)',\n",
    "    'location': '113.676866,23.631736',\n",
    "    'distance': '393.93',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;科教文化场所;科教文化场所'},\n",
    "   {'id': 'B0FFLBEOND',\n",
    "    'direction': '东北',\n",
    "    'businessarea': [],\n",
    "    'address': '从莞深高速(广州南方学院)',\n",
    "    'poiweight': '0.190963',\n",
    "    'name': '聚贤楼10号',\n",
    "    'location': '113.681253,23.631036',\n",
    "    'distance': '418.51',\n",
    "    'tel': [],\n",
    "    'type': '商务住宅;住宅区;宿舍'},\n",
    "   {'id': 'B0FFH6K3U9',\n",
    "    'direction': '西北',\n",
    "    'businessarea': [],\n",
    "    'address': '温泉镇温泉大道882号广州南方学院H7栋1-5层',\n",
    "    'poiweight': '0.254885',\n",
    "    'name': '广州中大南方招待所',\n",
    "    'location': '113.676330,23.631332',\n",
    "    'distance': '378.102',\n",
    "    'tel': '020-61787222',\n",
    "    'type': '住宿服务;住宿服务相关;住宿服务相关'},\n",
    "   {'id': 'B0HGD58NF8',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '温泉大道882号广州南方学院新综合楼2层',\n",
    "    'poiweight': '0.191229',\n",
    "    'name': '广州南方学院达人书院',\n",
    "    'location': '113.679041,23.632392',\n",
    "    'distance': '446.366',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;科教文化场所;科教文化场所'},\n",
    "   {'id': 'B0FFKUVYWV',\n",
    "    'direction': '东北',\n",
    "    'businessarea': [],\n",
    "    'address': '保利桃花源',\n",
    "    'poiweight': '0.273168',\n",
    "    'name': '广州和风庭阿七度假别墅',\n",
    "    'location': '113.681736,23.630569',\n",
    "    'distance': '424.35',\n",
    "    'tel': '18681565305',\n",
    "    'type': '住宿服务;住宿服务相关;住宿服务相关'},\n",
    "   {'id': 'B0HGDU99RM',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '温泉大道882号广州南方学院内',\n",
    "    'poiweight': '0.257947',\n",
    "    'name': '广州南方学院图书馆',\n",
    "    'location': '113.679177,23.632762',\n",
    "    'distance': '489.331',\n",
    "    'tel': '020-61787368;020-61787371',\n",
    "    'type': '科教文化服务;图书馆;图书馆'},\n",
    "   {'id': 'B0HGDU92JC',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '博学路附近',\n",
    "    'poiweight': '0.221662',\n",
    "    'name': '广州南方学院4号实验楼',\n",
    "    'location': '113.679963,23.632652',\n",
    "    'distance': '498.883',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'},\n",
    "   {'id': 'B0FFL7C054',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '温泉镇博学路882号(广州南方学院商业中心1层)',\n",
    "    'poiweight': '0.317505',\n",
    "    'name': '广州U优森特酒店',\n",
    "    'location': '113.679226,23.632528',\n",
    "    'distance': '464.819',\n",
    "    'tel': '15813395296',\n",
    "    'type': '住宿服务;住宿服务相关;住宿服务相关'},\n",
    "   {'id': 'B0FFG8CSEK',\n",
    "    'direction': '东北',\n",
    "    'businessarea': [],\n",
    "    'address': '从莞深高速(广州南方学院)',\n",
    "    'poiweight': '0.185371',\n",
    "    'name': 'L5员工宿舍',\n",
    "    'location': '113.682583,23.631487',\n",
    "    'distance': '554.129',\n",
    "    'tel': [],\n",
    "    'type': '商务住宅;住宅区;宿舍'},\n",
    "   {'id': 'B0HGD58FR2',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '广州南方学院',\n",
    "    'poiweight': '0.221662',\n",
    "    'name': '广州南方学院教学楼9号',\n",
    "    'location': '113.679747,23.633032',\n",
    "    'distance': '532.163',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'},\n",
    "   {'id': 'B0HG5Z9JNK',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '博学路',\n",
    "    'poiweight': '0.221553',\n",
    "    'name': '广州南方学院教学楼7号',\n",
    "    'location': '113.678600,23.633330',\n",
    "    'distance': '544.868',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'},\n",
    "   {'id': 'B0HGD599JM',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '温泉大道882号广州南方学院',\n",
    "    'poiweight': '0.221662',\n",
    "    'name': '广州南方学院第11教学楼',\n",
    "    'location': '113.680037,23.633173',\n",
    "    'distance': '556.036',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'},\n",
    "   {'id': 'B0GUJYPEG2',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '温泉大道882号广州南方学院内博学路6号之3',\n",
    "    'poiweight': '0.216026',\n",
    "    'name': '怒烤烤串(从化店)',\n",
    "    'location': '113.677160,23.632910',\n",
    "    'distance': '510.112',\n",
    "    'tel': [],\n",
    "    'type': '餐饮服务;中餐厅;特色/地方风味餐厅'},\n",
    "   {'id': 'B0HGDU6Z41',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '温泉大道882',\n",
    "    'poiweight': '0.221662',\n",
    "    'name': '广州南方学院教学楼6号',\n",
    "    'location': '113.678424,23.633710',\n",
    "    'distance': '586.331',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'},\n",
    "   {'id': 'B0HGD580NZ',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '博学路7号附近',\n",
    "    'poiweight': '0.285712',\n",
    "    'name': '广州南方学院惟贤门',\n",
    "    'location': '113.678437,23.633721',\n",
    "    'distance': '587.601',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;高等院校'},\n",
    "   {'id': 'B0HGDU7T1K',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '从莞深高速(广州南方学院)',\n",
    "    'poiweight': '0.191229',\n",
    "    'name': '广州南方学院教学楼10号',\n",
    "    'location': '113.680125,23.633492',\n",
    "    'distance': '592.504',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;科教文化场所;科教文化场所'},\n",
    "   {'id': 'B0HGDU76L1',\n",
    "    'direction': '北',\n",
    "    'businessarea': [],\n",
    "    'address': '中山路与博学路交叉口东南150米',\n",
    "    'poiweight': '0.221662',\n",
    "    'name': '广州南方学院教学楼5号',\n",
    "    'location': '113.678046,23.633982',\n",
    "    'distance': '616.848',\n",
    "    'tel': [],\n",
    "    'type': '科教文化服务;学校;学校'}]},\n",
    " 'info': 'OK',\n",
    " 'infocode': '10000'}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53001c00",
   "metadata": {},
   "source": [
    "-------\n",
    "第二层  广州南方学院_regeocode:仍是字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "50183531",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['status', 'regeocode', 'info', 'infocode'])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "广州南方学院.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e23ecb60",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name '广州南方学院_regeocode' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-10-d3414ef94b87>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0m广州南方学院_regeocode\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name '广州南方学院_regeocode' is not defined"
     ]
    }
   ],
   "source": [
    "广州南方学院_regeocode.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2f04c1dd",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name '广州南方学院_regeocode' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-11-769a39f6529c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0m广州南方学院_regeocode\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'roads'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name '广州南方学院_regeocode' is not defined"
     ]
    }
   ],
   "source": [
    "广州南方学院_regeocode['roads']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d1d3b65",
   "metadata": {},
   "source": [
    "上面从这个regeocode开始老说我的未定义，不知道咋弄了，留着这个问题去问智超老师"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "339020b3",
   "metadata": {},
   "source": [
    "下面一章会出答案的 然后看看研究"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "132be6eb",
   "metadata": {},
   "source": [
    "发现下一章的上课老师让我打着好好的能正常运行显示，然后一回宿舍自己原封不动地打开原封不动地却运行报错，我懵了"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
